#pragma once

class EditBox : public UIElement
{
	Texture* m_border;
	CFont m_text;
	std::string m_curText;
	std::wstring m_wcurText;
	bool mUseUnicode;
	Vertex3FRHWT m_vertices[4];
	Vertex3FRHWC m_linePoints[5];
	bool m_focus;
	ui32 m_lastChange;
	bool m_drawCursel;
public:
	EditBox(ui32 x, ui32 y, ui32 w, ui32 h);
	void OnInput(char c);
	void Draw();

	void SetText(std::string szText)
	{
		m_curText = szText;
		for(ui32 i = 0; i < 5; ++i)
			m_linePoints[i].x = m_position.x + 6 + m_text.GetTextLen(m_curText);
	}

	void SetText(std::wstring wszText)
	{
		m_wcurText = wszText;
		mUseUnicode = true;
		for(ui32 i = 0; i < 5; ++i)
			m_linePoints[i].x = m_position.x + 6 + m_text.GetTextLenW(m_wcurText);
	}

	void MouseActivity(bool left, Vertex2F pos, bool up)
	{
		if(!m_visible)
			return;
		if(!up)
			return;
		if(pos.x >= m_position.x && (pos.x <= m_position.x + m_size.x) &&
			pos.y >= m_position.y && (pos.y <= m_position.y + m_size.y))
		{
			m_focus = true;
		}
		else
			m_focus = false;
	}

	std::string GetText() { return m_curText; }

	void ModPosition(i32 dx, i32 dy)
	{
		m_position.x += dx;
		m_position.y += dy;

		m_vertices[0].x = m_position.x;
		m_vertices[0].y = m_position.y;

		m_vertices[1].x = m_position.x + m_size.x;
		m_vertices[1].y = m_position.y;

		m_vertices[2].x = m_position.x + m_size.x;
		m_vertices[2].y = m_position.y + m_size.y;

		m_vertices[3].x = m_position.x;
		m_vertices[3].y = m_position.y + m_size.y;

		m_linePoints[0].x = m_position.x + 6;
		m_linePoints[0].y = m_position.y + 5;
		m_linePoints[1].x = m_position.x + 7;
		m_linePoints[1].y = m_position.y + 5;
		m_linePoints[2].x = m_position.x + 7;
		m_linePoints[2].y = m_position.y + m_size.y - 5;
		m_linePoints[3].x = m_position.x + 6;
		m_linePoints[3].y = m_position.y + m_size.y - 5;
		m_linePoints[4] = m_linePoints[0];

		for(ui32 i = 0; i < 5; ++i)
			m_linePoints[i].x = m_position.x + 6 + m_text.GetTextLen(m_curText, true);
	}

	bool GetFocus() { return m_focus; }
};